package com.sctvcloud.demo1.utils;

import android.content.Context;
import android.text.TextUtils;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.jiongbull.jlog.JLog;
import com.sctvcloud.demo1.application.Cache;

import java.util.HashSet;
import java.util.Set;

/**
 * 百度定位
 */
public class LocationUtils {
    private Context context;
    private static final String TAG = "LocationUtils";
    private LocationClient mLocationClient = null;
    private BDLocationListener myListener = new MyLocationListener();

    private Set<OnLocatedListener> onLocatedListeners = new HashSet<OnLocatedListener>();
    private static LocationUtils instance;

    private LocationUtils() {
    }

    public static synchronized LocationUtils getInstance() {
        if (null == instance) {
            instance = new LocationUtils();
        }
        return instance;
    }

    private void init(Context context) {
        this.context = context;
        if (null == mLocationClient) {
            mLocationClient = new LocationClient(context); // 声明LocationClient类
            mLocationClient.registerLocationListener(myListener); // 注册监听函数
            LocationClientOption option = new LocationClientOption();
            option.setLocationMode(LocationClientOption.LocationMode.Battery_Saving);// 设置定位模式：省电、高精度、仅设备
            option.setCoorType("bd09ll");// 返回的定位结果是百度经纬度，默认值gcj02
            // option.setScanType(5000);//设置发起定位请求的间隔时间为5000ms
            option.setOpenGps(true);//打开Gps
//            option.setScanSpan(0);//可选，默认0，即仅定位一次，设置发起定位请求的间隔需要大于等于1000ms才是有效的
            option.setAddrType("all");
            option.setIgnoreKillProcess(false);//可选，默认true，定位SDK内部是一个SERVICE，并放到了独立进程，设置是否在stop的时候杀死这个进程，默认不杀死
            option.setIsNeedAddress(true);// 返回的定位结果包含地址信息
            // option.setNeedDeviceDirect(true);//返回的定位结果包含手机机头的方向
            mLocationClient.setLocOption(option);
        }
        if (!mLocationClient.isStarted()) {
            mLocationClient.start();
        }
    }

    /**
     * 启动定位
     *
     * @param context
     */
    public void startLocate(Context context) {
        init(context);
        if (mLocationClient != null && mLocationClient.isStarted()) {
            mLocationClient.requestLocation();
        } else {
            JLog.e(TAG + "locClient is null or not started");
        }
    }

    public void stopLocate() {
        if (null != mLocationClient) {
            JLog.e(TAG + "locClient is stoped");
            mLocationClient.stop();
        }
    }


    public class MyLocationListener implements BDLocationListener {
        @Override
        public void onReceiveLocation(BDLocation location) {
            JLog.e(TAG + "Loaction back");
            if (location == null) {
                mLocationClient.requestLocation();
                return;
            } else if (null != mLocationClient) {
                mLocationClient.stop();
                mLocationClient = null;
            }

            JLog.e(TAG + "|省|" + location.getProvince() + "|城市|" + location.getCity() + "  code=" + location.getCityCode());

            if (!TextUtils.isEmpty(location.getProvince())
                    && !TextUtils.isEmpty(location.getCity())
                    && !TextUtils.isEmpty(location.getCityCode())) {

                Cache.getInstance().setProvince(location.getProvince());
                Cache.getInstance().setCurrentCity(location.getCity());
                Cache.getInstance().setCurrentCityCode(location.getCityCode());
//                Cache.getInstance().setCurrentLatitude(location.getLatitude());
//                Cache.getInstance().setCurrentLongitude(location.getLongitude());

            }
            if(null!=onLocatedListeners) {
                for (OnLocatedListener l : onLocatedListeners) {
                    if (null != l) {
                        l.OnLocated(location);
                    }
                }
            }

        }
    }

    public void registerOnLocatedListener(OnLocatedListener l) {
        unregisterOnLocatedListener(l);
        onLocatedListeners.add(l);
    }

    public void unregisterOnLocatedListener(OnLocatedListener l) {
        if (onLocatedListeners.contains(l)) {
            onLocatedListeners.remove(l);
        }
    }

    public interface OnLocatedListener {
        public void OnLocated(BDLocation location);
    }
}
